package org.csu.mypetstorebackstage.service;

import org.csu.mypetstorebackstage.domain.Account;
import org.csu.mypetstorebackstage.domain.Log;
import org.csu.mypetstorebackstage.persistence.AccountMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class AccountService {
    @Autowired
    private AccountMapper accountMapper;

    public Account getAccountByUserId(String userId){return accountMapper.getAccountByUserId(userId);}

    public List<Account> getAllAccounts(){return accountMapper.getAllAccounts();}

    public void deleteAccountByUserId(String userId){
        accountMapper.deleteSignonByUserId(userId);
        accountMapper.deleteAccountByUserId(userId);
        accountMapper.deleteProfileByUserId(userId);
        accountMapper.deleteAddLogsByUserId(userId);
        accountMapper.deleteBrowseLogsByUserId(userId);
    }

    public void resetPasswordByUserId(String userId){
        Account account = accountMapper.getAccountByUserId(userId);
        account.setPassword("123456");
        accountMapper.updateAccount(account);
    }

    public void updateAccountByUserId(Account account){accountMapper.updateAccount(account);}

    public List<Log> getAlllogs(String userId){
        List<Log> logList = new ArrayList<Log>();
        List<Log> browseloglist = accountMapper.getBrowseLogsByUserId(userId);
        List<Log> addloglist = accountMapper.getAddLogsByUserId(userId);
        logList.addAll(browseloglist);
        logList.addAll(addloglist);
        return logList;
    }

    public List<Log> getBrowseLogs(String userId){return accountMapper.getBrowseLogsByUserId(userId);}
    public List<Log> getAddLogs(String userId){return  accountMapper.getAddLogsByUserId(userId);}

    public String getPasswordByUserId(String userId){return accountMapper.getPasswordByUserId(userId);}

    public void insertAccount(Account account){
        accountMapper.insertAccount(account);
        accountMapper.insertSignon(account);
        accountMapper.insertProfile(account);
    }

    public void deleteAllLogsByUserId(String userId){
        accountMapper.deleteAddLogsByUserId(userId);
        accountMapper.deleteBrowseLogsByUserId(userId);
    }

    public void deleteBrowseLogById(String id){accountMapper.deleteBrowseLogById(id);}
    public void deleteAddLogById(String id){accountMapper.deleteAddLogById(id);}
}
